<?php
  $objXajax->registerFunction("fmfrmroomtype_save");
  $objXajax->registerFunction("fmfrmroomtype_edit");
  $objXajax->registerFunction("fmfrmroomtype_delete");
  $objXajax->registerFunction("fmfrmroomtype_loadGrid");
  function fmfrmroomtype_save($params){     
    global $objDbUpdate,$user_id,$hotel_id;
    $objResponse = new xajaxResponse();
    $name = addslashes($params['txtnametype']);
    $style = $params['cbbstyle'];
    $minmoney = $params['txtminmoney']; 
    $low = $params['low'];
    $high = $params['high']; 

    $price_1_1= $params['txtprice-1-1']; 
    $price_1_2= $params['txtprice-1-2']; 
    $price_1_3= $params['txtprice-1-3']; 
    $price_2_1= $params['txtprice-2-1']; 
    $price_2_2= $params['txtprice-2-2']; 
    $price_2_3= $params['txtprice-2-3']; 
    $price_3_1= $params['txtprice-3-1']; 
    $price_3_2= $params['txtprice-3-2']; 
    $price_3_3= $params['txtprice-3-3']; 
    $price_4_1= $params['txtprice-4-1']; 
    $price_4_2= $params['txtprice-4-2']; 
    $price_4_3= $params['txtprice-4-3']; 
    $price_5_1= $params['txtprice-5-1']; 
    $price_5_2= $params['txtprice-5-2']; 
    $price_5_3= $params['txtprice-5-3']; 
    $price_6_1= $params['txtprice-6-1']; 
    $price_6_2= $params['txtprice-6-2']; 
    $price_6_3= $params['txtprice-6-3']; 
    $price_0_1= $params['txtprice-0-1']; 
    $price_0_2= $params['txtprice-0-2']; 
    $price_0_3= $params['txtprice-0-3']; 

    $id = $params['hdId'];
    $edit = $params['hdEdit'];
    $flag = true;

    
    if($params['hdEdit']==1){
        $sqlUpdate = "update fm_room_type set name ='$name', style='$style',minmoney='$minmoney',
        date_update=now(),user_update='$user_id' WHERE id='$id' ";
        $flag = $objDbUpdate->Execute($sqlUpdate);    
        $sqldelete = "delete from fm_room_price WHERE room_type_id = $id" ;
        $objDbUpdate->Execute($sqldelete); 
          
 
    }else{
        $sqlInsert = "insert into fm_room_type(name,style,date_created,date_update,user_created,user_update,service_id,minmoney)
        value('$name','$style',now(),now(),'$username','$username',$hotel_id,'$minmoney')";
        $flag = $objDbUpdate->Execute($sqlInsert);
        $id = mysql_insert_id();
        $objResponse->addScript("changeUpdate('fmfrmroomtype',$id);");  
    }
     $sql = "INSERT INTO fm_room_price(room_type_id,timefrom,timeto,price,dateofweek,service_id) values";
     $sql.="($id,0,$low,$price_1_1,1,$hotel_id)";
     $sql.=",($id,$low,$high,$price_1_2,1,$hotel_id)";
     $sql.=",($id,$high,24,$price_1_3,1,$hotel_id)";
     
     $sql.=",($id,0,$low,$price_2_1,2,$hotel_id)";
     $sql.=",($id,$low,$high,$price_2_2,2,$hotel_id)";
     $sql.=",($id,$high,24,$price_2_3,2,$hotel_id)";
     
     $sql.=",($id,0,$low,$price_3_1,3,$hotel_id)";
     $sql.=",($id,$low,$high,$price_3_2,3,$hotel_id)";
     $sql.=",($id,$high,24,$price_3_3,3,$hotel_id)";
     
     $sql.=",($id,0,$low,$price_4_1,4,$hotel_id)";
     $sql.=",($id,$low,$high,$price_4_2,4,$hotel_id)";
     $sql.=",($id,$high,24,$price_4_3,4,$hotel_id)";
     
     $sql.=",($id,0,$low,$price_5_1,5,$hotel_id)";
     $sql.=",($id,$low,$high,$price_5_2,5,$hotel_id)";
     $sql.=",($id,$high,24,$price_5_3,5,$hotel_id)";
     
     $sql.=",($id,0,$low,$price_6_1,6,$hotel_id)";
     $sql.=",($id,$low,$high,$price_6_2,6,$hotel_id)";
     $sql.=",($id,$high,24,$price_6_3,6,$hotel_id)";
     
     $sql.=",($id,0,$low,$price_0_1,0,$hotel_id)";     
     $sql.=",($id,$low,$high,$price_0_2,0,$hotel_id)";
     $sql.=",($id,$high,24,$price_0_3,0,$hotel_id)";
     $objDbUpdate->Execute($sql);
     $objResponse->addScript('xajax_fmfrmroomtype_loadGrid();');
     $objResponse->addScript("checkUpdateStatus(1,'fmfrmroomtype','$edit');");
     $objResponse->addScript("hasChange= true;");     
     return $objResponse->getXML(); 
  }
  
   function fmfrmroomtype_loadGrid(){
    global $lang;
    $objResponse = new xajaxResponse();
    $arrGrid = fmfrmroomtype_GetGrid();
    $oTbs = new clsTinyButStrong();
    $oTbs->LoadTemplate('templates/'.$lang.'/customer/fm_roomtype_grid.html');
    $oTbs->MergeBlock('blk_data', $arrGrid);
    $oTbs->Show(TBS_NOTHING);
    $objResponse->addAssign('fmfrmroomtype_divGrid', 'innerHTML', $oTbs->Source);
    $objResponse->addScriptCall("dataTableObj.CreatDataTable('fmfrmroomtype_tbl');");
    $objResponse->addScript('changeWaiting(1);');                               
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax   
  }
  
    function  fmfrmroomtype_GetGrid(){
        global $objDbSelect,$hotel_id;
        $sqlSelect = "select * from fm_room_type WHERE service_id ='$hotel_id'  order by name";
        return $objDbSelect->GetArray($sqlSelect);
    }
    function fmfrmroomtype_delete($strId){
        global $objDbSelect;
       $objResponse = new xajaxResponse();
       $sql = " delete from fm_room where room_type in ($strId) " ;
       if($objDbSelect->Execute($sql)){
           $sql = " delete from fm_room_price where room_type_id in ($strId) " ;
           $objDbSelect->Execute($sql); 
           $sql = " delete from fm_room_type where id in ($strId) " ; 
           $objDbSelect->Execute($sql);
       }
       $objResponse->addScript('changeWaiting(1);');
       $objResponse->addScript('deleteRowlogic();');
       return $objResponse->getXML(); // dong luon luon co khi goi ham ajax  
    }
    
    function fmfrmroomtype_edit($id){
       global $objDbSelect;
       $objResponse = new xajaxResponse();
       $sql = " SELECT * 
       FROM fm_room_type WHERE id = '$id' ";
      // echo $sql; die();
       $arr = $objDbSelect->GetArray($sql);      
       
      $style        = $arr[0]['style'];
      $name         = $arr[0]['name'];                                              
      $minmoney         =FormatMoney($arr[0]['minmoney']) ;
      
      $description =htmlspecialchars_decode($arr[0]['description']) ;
      $objResponse->addAssign('fmfrmroomtype_description','value',$description);                                              
                                          
      $objResponse->addScript("$('#fmfrmroomtype_txtnametype').val('$name');");
      $objResponse->addScript("$('#fmfrmroomtype_cbbstyle-$style').attr('checked',true);");
      $objResponse->addScript("$('#fmfrmroomtype_txtminmoney').val('$minmoney');"); 
      $sql = " select * from fm_room_price where room_type_id = '$id' order by dateofweek,timefrom "  ;
      $arr = $objDbSelect->GetArray($sql);
      $flg =0;
      $value0 = 6;
      $value1 = 18;
      $price1 = 0;
      $price2 = 0;
      $price3 = 0;
      for($i=0;$i<count($arr);$i++){
          $dateofweek = $arr[$i]['dateofweek'];
          if($flg==0){
              $value0= $arr[$i]['timeto'];
              $price1 = FormatMoney($arr[$i]['price']);
              $flg++;
          }elseif($flg==1){
              $value1= $arr[$i]['timeto'];  
              $price2= FormatMoney($arr[$i]['price']);
              $flg++;  
          }else{
              $price3= FormatMoney($arr[$i]['price']);
              $flg =0;
              $objResponse->addScript("fmfrmroomtype_SetSlider($dateofweek,$value0,$value1,'$price1','$price2','$price3');");  
          }
      }
      $objResponse->addScript('changeWaiting(1);');
      return $objResponse->getXML();   
    }
?>
